/**
  说明: 登录流程
  1. 收集表单项数据
  2. 前端验证
    1) 验证用户信息(账号，密码)是否合法
    2) 前端验证不通过就提示用户，不需要发请求给后端
    3) 前端验证通过了，发请求(携带账号, 密码)给服务器端
  3. 后端验证
    1) 验证用户是否存在
    2) 用户不存在直接返回，告诉前端用户不存在
    3) 用户存在需要验证密码是否正确
    4) 密码不正确返回给前端提示密码不正确
    5) 密码正确返回给前端数据，提示用户登录成功(会携带用户的相关信息)
*/
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 手机号和密码
    phone: '',
    password: ''
  },
  // 表单项事件的回调
  handleInput(event){
    /**
     * event.target VS event.currentTarget
     *   (1) event.target 绑定事件的对象不一定是触发事件的对象，比如: 事件委托
     *   (2) event.currentTarget 绑定事件的对象一定是触发事件的对象
     */
    // 使用 id 方式确定元素,适合传唯一标识
    // const type = event.currentTarget.id
    // 使用 data- 方式确定元素,适合传多个值,一个标签上可以有多个 data-
    const type = event.currentTarget.dataset.type
    this.setData({
      [type]: event.detail.value
    })
  },
  // 登录的回调
  async login(){
    // 收集表单项数据
    const {phone,password} = this.data
    // 前端验证
    if(!phone){
      // 提示用户
      wx.showToast({
        title: '手机号不能为空',
        icon: 'error'
      })
      return
    }
    // 验证手机号格式
    const phoneReg = /^[1][3,4,5,6.7,8,9][0-9]{9}$/
    if(!phoneReg.test(phone)){
      wx.showToast({
        title: '手机号格式错误',
        icon: 'error'
      })
      return
    }
    // 验证密码
    if(!password){
      wx.showToast({
        title: '密码不能为空',
        icon: 'error'
      })
      return
    }
    // 提交
    let result = await request('/login/cellphone',{phone,password,isLogin:true})
    result = {"loginType":1,"code":200,"account":{"id":2046710473,"userName":"1_13775215621","type":1,"status":0,"whitelistAuthority":0,"createTime":1573914439058,"salt":"[B@7b1f459c","tokenVersion":1,"ban":0,"baoyueVersion":0,"donateVersion":0,"vipType":0,"viptypeVersion":0,"anonimousUser":false,"uninitialized":false},"token":"759416ce47560dd5b994c647bf8d07ce8182fe18fe02ee0f168f3cf90532aa0b519e07624a9f00539413fe7a5481a608d8aaa7883321718ce0feb51ff6cfefef2c2fc7ba504a7c19d4dbf082a8813684","profile":{"followed":false,"backgroundUrl":"https://p2.music.126.net/2zSNIqTcpHL2jIvU6hG0EA==/109951162868128395.jpg","detailDescription":"","avatarImgIdStr":"109951165647004069","backgroundImgIdStr":"109951162868128395","userId":2046710473,"userType":0,"accountStatus":0,"vipType":0,"gender":0,"avatarImgId":109951165647004069,"nickname":"二营长谷子地","backgroundImgId":109951162868128395,"birthday":-2209017600000,"city":320500,"avatarUrl":"https://p2.music.126.net/SUeqMM8HOIpHv9Nhl9qt9w==/109951165647004069.jpg","defaultAvatar":false,"province":320000,"expertTags":null,"experts":{},"mutual":false,"remarkName":null,"authStatus":0,"djStatus":0,"description":"","signature":"","authority":0,"avatarImgId_str":"109951165647004069","followeds":0,"follows":3,"eventCount":0,"avatarDetail":null,"playlistCount":1,"playlistBeSubscribedCount":0},"bindings":[{"userId":2046710473,"url":"","expired":false,"bindingTime":1573956468731,"tokenJsonStr":"{\"countrycode\":\"\",\"cellphone\":\"13775215621\",\"hasPassword\":true}","expiresIn":2147483647,"refreshTime":1573956468,"id":6994837068,"type":1}]}
    
    if(result.code === 200){
      wx.showToast({
        title: '登录成功',
      })
      // 将用户信息存入本地
      wx.setStorageSync('userInfo',result.profile)

      // 跳转至个人中心页面
      // 关闭所有页面，打开到应用内的某个页面
      wx.reLaunch({
        url: '/pages/personal/personal',
      })
    }else if(result.code === 400){
      wx.showToast({
        title: '手机号错误',
        icon: 'error'
      })
    }else if(result.code === 502){
      wx.showToast({
        title: '密码错误',
        icon: 'error'
      })
    }else{
      wx.showToast({
        title: '网络繁忙',
        icon: 'error'
      })
    }
  },
   /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})